package com.sikaryofficial.backend.controller.prize;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sikaryofficial.backend.annotation.Repeat;
import com.sikaryofficial.backend.domain.dto.req.prize.PrizeAddressReq;
import com.sikaryofficial.backend.domain.dto.req.prize.PrizeListReq;
import com.sikaryofficial.backend.domain.dto.req.prize.PrizeOwnerListReq;
import com.sikaryofficial.backend.domain.dto.req.prize.PrizeRedeemReq;
import com.sikaryofficial.backend.domain.dto.resp.prize.PrizeDTO;
import com.sikaryofficial.backend.domain.dto.resp.prize.PrizeRedeemDTO;
import com.sikaryofficial.backend.manager.PrizeManager;
import com.sikaryofficial.common.core.domain.R;
import com.sikaryofficial.common.core.utils.PageUtil;
import com.sikaryofficial.common.core.web.controller.BaseController;
import com.sikaryofficial.common.core.web.domain.PageResult;
import com.sikaryofficial.common.security.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * 奖品Controller
 *
 * @author qinjinyuan
 * @date 2024-07-12
 */
@RestController
@RequestMapping("/prize/frontend")
@Api(tags = "V3.5-礼品H5")
@Slf4j
public class PrizeFrontendController extends BaseController {
    @Autowired
    private PrizeManager prizeManager;

    /**
     * 奖品兑换列表
     */
    @PostMapping(value = "/owner/redeem/pageList")
    @ApiOperation("个人中心-奖品兑换列表")
    public PageResult<PrizeRedeemDTO> ownerRedeemList(@Validated @RequestBody PrizeOwnerListReq req) {
        // 查询获奖列表
        IPage<PrizeRedeemDTO> page = prizeManager.ownerRedeemList(req);
        return PageUtil.success(page.getRecords(), page.getTotal());
    }

    @GetMapping(value = "/owner/redeem/detail/{businessType}/{objectId}")
    @ApiOperation("个人中心-中奖详情")
    public R<PrizeRedeemDTO> ownerRedeemDetail(
            @ApiParam(value = "业务类型 1-积分兑换,2-大转盘,3-刮刮乐,4-积分预约0元定时抢购", required = true)
            @PathVariable("businessType") Integer businessType,
            @ApiParam(value = "对象ID", required = true)
            @PathVariable("objectId") Long objectId) {
        return R.ok(prizeManager.ownerRedeemDetail(businessType, objectId));
    }

    @PostMapping(value = "/owner/redeem/address")
    @ApiOperation("个人中心-奖品兑换列表-地址更新")
    @Repeat(60)
    public R<Void> updateLotteryResultAddress(@Validated @RequestBody PrizeAddressReq req) {
        // 只能自己修改自己的地址
        if (Objects.equals(0L, SecurityUtils.getUserId()) || !SecurityUtils.getUserId().equals(req.getUserId())) {
            return R.fail("You can only modify your own address");
        }
        // 查询获奖列表
        prizeManager.updateLotteryResultAddress(req);
        return R.ok();
    }

    /**
     * 查询礼品分页列表
     */
    @PostMapping("/listPage")
    @ApiOperation("H5礼品分页列表")
    public PageResult<PrizeDTO> listPage(@Validated @RequestBody PrizeListReq req) {
        // 查询获奖列表
        IPage<PrizeDTO> page = prizeManager.listPageForH5(req);
        return PageUtil.success(page.getRecords(), page.getTotal());
    }

    /**
     * 获取奖品详细信息
     */
    @GetMapping(value = "/detail/{prizeId}")
    @ApiOperation("H5奖品兑换详情")
    public R<PrizeDTO> getInfo(@PathVariable("prizeId") Long prizeId) {
        return R.ok(prizeManager.getInfoForH5(prizeId));
    }

    /**
     * 目前只支持兑换一个
     *
     * @param req
     * @return
     */
    @PostMapping(value = "/redeem")
    @ApiOperation("H5奖品兑换")
    public R<Boolean> redeem(@Validated @RequestBody PrizeRedeemReq req) {
        // 目前只支持一个数量的兑换
        req.setPrizeNum(1);
        return R.ok(prizeManager.redeem(req));
    }

}
